﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Drawing;
using HandBook.Data;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Admin_Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //(DetailsView1.FindControl("RadEditor1") as RadEditor).EditModes = (DetailsView1.FindControl("RadEditor1") as RadEditor).EditModes ^ Telerik.Web.UI.EditModes.Design;
        if (!this.IsPostBack)
        {
            if (Button1.Text == "Edit")
            {

                HtmlGenericControl RadEditor1 = ((HtmlGenericControl)DetailsView1.FindControl("divLongDescription"));
                RadEditor1.Style.Add("display", "none");

                Label lbLongDescription = ((Label)DetailsView1.FindControl("lbLongDescription"));
                lbLongDescription.Visible = true;

                HtmlGenericControl RadEditor2 = ((HtmlGenericControl)DetailsView1.FindControl("divMenu"));
                RadEditor2.Style.Add("display", "none");

                Label lbMenu = ((Label)DetailsView1.FindControl("lbMenu"));
                lbMenu.Visible = true;
            }
            else
            {
                HtmlGenericControl RadEditor1 = ((HtmlGenericControl)DetailsView1.FindControl("divLongDescription"));
                RadEditor1.Style.Add("display", "");

                Label lbLongDescription = ((Label)DetailsView1.FindControl("lbLongDescription"));
                lbLongDescription.Visible = false;

                HtmlGenericControl RadEditor2 = ((HtmlGenericControl)DetailsView1.FindControl("divMenu"));
                RadEditor2.Style.Add("display", "");

                Label lbMenu = ((Label)DetailsView1.FindControl("lbMenu"));
                lbMenu.Visible = false;
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Button1.Text == "Edit")
        {
            SetControlsStyle();

            Button1.Text = "Submit";
        }
        else
        {
            UpdateRestaurants();
            
            ScriptManager.RegisterStartupScript(this, GetType(), "closeWnd", "CloseWnd();", true);
        }

        if (Button1.Text == "Edit")
        {

            HtmlGenericControl RadEditor1 = ((HtmlGenericControl)DetailsView1.FindControl("divLongDescription"));
            RadEditor1.Style.Add("display", "none");

            Label lbLongDescription = ((Label)DetailsView1.FindControl("lbLongDescription"));
            lbLongDescription.Visible = true;

            HtmlGenericControl RadEditor2 = ((HtmlGenericControl)DetailsView1.FindControl("divMenu"));
            RadEditor2.Style.Add("display", "none");

            Label lbMenu = ((Label)DetailsView1.FindControl("lbMenu"));
            lbMenu.Visible = true;
        }
        else
        {
            HtmlGenericControl RadEditor1 = ((HtmlGenericControl)DetailsView1.FindControl("divLongDescription"));
            RadEditor1.Style.Add("display", "");

            Label lbLongDescription = ((Label)DetailsView1.FindControl("lbLongDescription"));
            lbLongDescription.Visible = false;

            HtmlGenericControl RadEditor2 = ((HtmlGenericControl)DetailsView1.FindControl("divMenu"));
            RadEditor2.Style.Add("display", "");

            Label lbMenu = ((Label)DetailsView1.FindControl("lbMenu"));
            lbMenu.Visible = false;

            RadEditor rad2 = DetailsView1.FindControl("RadEditor2") as RadEditor;
            rad2.Width = Unit.Pixel(700);
            if (rad2.Content == "")
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("<table width=\"663\" border=\"1\">");
                builder.Append("<tr align=\"center\">");
                builder.Append("<td style=\"font-weight: bold; width: 331px\">Nhập vào tên món ăn ở đây</td>");
                builder.Append("<td style=\"font-weight: bold; width: 332px\">Nhập vào giá tiền tương ứng với món ăn ở đây</td>");
                builder.Append("</tr>");
                for (int i = 0; i < 1000; i++)
                {
                    builder.Append("<tr align=\"center\">");
                    builder.Append("<td>&nbsp;</td>");
                    builder.Append("<td>&nbsp;</td>");
                    builder.Append("</tr>");
                }
                builder.Append("</table>");

                rad2.Content = builder.ToString();
            }
        }
    }

    private void UpdateRestaurants()
    {
        int restID = Convert.ToInt32(Request.Params["RestID"].ToString());
        var rest = HandBookData.EatDrinkDB.Restaurants.Single(p => p.RestID == restID);

        rest.RestName = (DetailsView1.FindControl("txtRestName") as TextBox).Text.Trim();
        rest.RestCat = (DetailsView1.FindControl("txtRestCat") as TextBox).Text.Trim();
        rest.Address = (DetailsView1.FindControl("txtAddress") as TextBox).Text.Trim();
        rest.AddressDescription = (DetailsView1.FindControl("txtAddressDescription") as TextBox).Text.Trim();
        rest.Slogan = (DetailsView1.FindControl("txtSlogan") as TextBox).Text.Trim();
        rest.ShortDescription = (DetailsView1.FindControl("txtShortDescription") as TextBox).Text.Trim();
        rest.LongDescription = (DetailsView1.FindControl("RadEditor1") as RadEditor).Content.Trim();
        rest.LocationID = Convert.ToInt32((DetailsView1.FindControl("ddlLocation") as DropDownList).SelectedValue);
        rest.PhoneNumber = (DetailsView1.FindControl("txtPhoneNumber") as TextBox).Text.Trim();
        rest.Email = (DetailsView1.FindControl("txtEmail") as TextBox).Text.Trim();
        rest.Fax = (DetailsView1.FindControl("txtFax") as TextBox).Text.Trim();
        rest.Website = (DetailsView1.FindControl("txtWebsite") as TextBox).Text.Trim();
        rest.SeatCount = (DetailsView1.FindControl("txtSeatCount") as TextBox).Text.Trim();
        rest.IsOrder = (DetailsView1.FindControl("ddlIsOrder") as DropDownList).SelectedValue.ToString();
        rest.OffDate = (DetailsView1.FindControl("txtOffDate") as TextBox).Text.Trim();
        rest.ServedTime = (DetailsView1.FindControl("txtServedTime") as TextBox).Text.Trim();
        rest.PaymentType = (DetailsView1.FindControl("txtPaymentType") as TextBox).Text.Trim();
        rest.Language = (DetailsView1.FindControl("txtLanguage") as TextBox).Text.Trim();
        rest.AveragePrice = (DetailsView1.FindControl("txtAveragePrice") as TextBox).Text.Trim();
        rest.SubServices = (DetailsView1.FindControl("txtSubServices") as TextBox).Text.Trim();
        rest.Menu = (DetailsView1.FindControl("RadEditor2") as RadEditor).Content.Trim();
        rest.Note = (DetailsView1.FindControl("txtNote") as TextBox).Text.Trim();
        rest.Search = (DetailsView1.FindControl("txtSearch") as TextBox).Text.Trim();
        rest.ImageFolderName = (DetailsView1.FindControl("txtImageFolderName") as TextBox).Text.Trim();
        rest.Flash = (DetailsView1.FindControl("txtFlash") as TextBox).Text.Trim();

        string orderContent = (DetailsView1.FindControl("txtOrder") as TextBox).Text.Trim();
        if (!orderContent.Equals(""))
        {
            rest.Order = Convert.ToInt32(orderContent);
        }
        else
        {
            rest.Order = null;
        }

        string userIDContent = (DetailsView1.FindControl("txtUserID") as TextBox).Text.Trim();
        if (!userIDContent.Equals(""))
        {
            rest.UserID = Convert.ToInt32(userIDContent);
        }
        else
        {
            rest.UserID = null;
        }

        rest.IsActivated = Convert.ToBoolean((DetailsView1.FindControl("ddlIsActivated") as DropDownList).SelectedValue.ToString());
        rest.Url = (DetailsView1.FindControl("txtUrl") as TextBox).Text.Trim();

        HandBookData.EatDrinkDB.SubmitChanges();
    }

    private void SetControlsStyle()
    {
        TextBox restName = (DetailsView1.FindControl("txtRestName") as TextBox);
        restName.BorderStyle = BorderStyle.Solid;
        restName.BorderWidth = Unit.Pixel(1);
        restName.BorderColor = Color.BlueViolet;
        restName.Enabled = true;

        TextBox restCat = (DetailsView1.FindControl("txtRestCat") as TextBox);
        restCat.BorderStyle = BorderStyle.Solid;
        restCat.BorderWidth = Unit.Pixel(1);
        restCat.BorderColor = Color.BlueViolet;
        restCat.Enabled = true;

        TextBox address = (DetailsView1.FindControl("txtAddress") as TextBox);
        address.BorderStyle = BorderStyle.Solid;
        address.BorderWidth = Unit.Pixel(1);
        address.BorderColor = Color.BlueViolet;
        address.Enabled = true;

        TextBox addressDescription = (DetailsView1.FindControl("txtAddressDescription") as TextBox);
        addressDescription.BorderStyle = BorderStyle.Solid;
        addressDescription.BorderWidth = Unit.Pixel(1);
        addressDescription.BorderColor = Color.BlueViolet;
        addressDescription.Enabled = true;

        TextBox slogan = (DetailsView1.FindControl("txtSlogan") as TextBox);
        slogan.BorderStyle = BorderStyle.Solid;
        slogan.BorderWidth = Unit.Pixel(1);
        slogan.BorderColor = Color.BlueViolet;
        slogan.Enabled = true;

        TextBox shortDescription = (DetailsView1.FindControl("txtShortDescription") as TextBox);
        shortDescription.BorderStyle = BorderStyle.Solid;
        shortDescription.BorderWidth = Unit.Pixel(1);
        shortDescription.BorderColor = Color.BlueViolet;
        shortDescription.Enabled = true;

        RadEditor RadEditor1 = ((RadEditor)DetailsView1.FindControl("RadEditor1"));
        RadEditor1.Enabled = true;
        RadEditor1.Style.Clear();
        RadEditor1.CssFiles.Add("nonexisting.css");

        TextBox phone = (DetailsView1.FindControl("txtPhoneNumber") as TextBox);
        phone.BorderStyle = BorderStyle.Solid;
        phone.BorderWidth = Unit.Pixel(1);
        phone.BorderColor = Color.BlueViolet;
        phone.Enabled = true;

        TextBox email = (DetailsView1.FindControl("txtEmail") as TextBox);
        email.BorderStyle = BorderStyle.Solid;
        email.BorderWidth = Unit.Pixel(1);
        email.BorderColor = Color.BlueViolet;
        email.Enabled = true;

        TextBox fax = (DetailsView1.FindControl("txtFax") as TextBox);
        fax.BorderStyle = BorderStyle.Solid;
        fax.BorderWidth = Unit.Pixel(1);
        fax.BorderColor = Color.BlueViolet;
        fax.Enabled = true;

        TextBox web = (DetailsView1.FindControl("txtWebsite") as TextBox);
        web.BorderStyle = BorderStyle.Solid;
        web.BorderWidth = Unit.Pixel(1);
        web.BorderColor = Color.BlueViolet;
        web.Enabled = true;

        TextBox seatCount = (DetailsView1.FindControl("txtSeatCount") as TextBox);
        seatCount.BorderStyle = BorderStyle.Solid;
        seatCount.BorderWidth = Unit.Pixel(1);
        seatCount.BorderColor = Color.BlueViolet;
        seatCount.Enabled = true;

        DropDownList loc = DetailsView1.FindControl("ddlLocation") as DropDownList;
        loc.Enabled = true;

        DropDownList isorder = DetailsView1.FindControl("ddlIsOrder") as DropDownList;
        isorder.Enabled = true;

        TextBox offDate = (DetailsView1.FindControl("txtOffDate") as TextBox);
        offDate.BorderStyle = BorderStyle.Solid;
        offDate.BorderWidth = Unit.Pixel(1);
        offDate.BorderColor = Color.BlueViolet;
        offDate.Enabled = true;

        TextBox servedTime = (DetailsView1.FindControl("txtServedTime") as TextBox);
        servedTime.BorderStyle = BorderStyle.Solid;
        servedTime.BorderWidth = Unit.Pixel(1);
        servedTime.BorderColor = Color.BlueViolet;
        servedTime.Enabled = true;

        TextBox payment = (DetailsView1.FindControl("txtPaymentType") as TextBox);
        payment.BorderStyle = BorderStyle.Solid;
        payment.BorderWidth = Unit.Pixel(1);
        payment.BorderColor = Color.BlueViolet;
        payment.Enabled = true;

        TextBox language = (DetailsView1.FindControl("txtLanguage") as TextBox);
        language.BorderStyle = BorderStyle.Solid;
        language.BorderWidth = Unit.Pixel(1);
        language.BorderColor = Color.BlueViolet;
        language.Enabled = true;

        TextBox price = (DetailsView1.FindControl("txtAveragePrice") as TextBox);
        price.BorderStyle = BorderStyle.Solid;
        price.BorderWidth = Unit.Pixel(1);
        price.BorderColor = Color.BlueViolet;
        price.Enabled = true;

        TextBox subService = (DetailsView1.FindControl("txtSubServices") as TextBox);
        subService.BorderStyle = BorderStyle.Solid;
        subService.BorderWidth = Unit.Pixel(1);
        subService.BorderColor = Color.BlueViolet;
        subService.Enabled = true;

        RadEditor RadEditor2 = ((RadEditor)DetailsView1.FindControl("RadEditor2"));
        RadEditor2.Enabled = true;
        RadEditor2.Style.Clear();
        RadEditor2.CssFiles.Add("nonexisting.css");

        TextBox note = (DetailsView1.FindControl("txtNote") as TextBox);
        note.BorderStyle = BorderStyle.Solid;
        note.BorderWidth = Unit.Pixel(1);
        note.BorderColor = Color.BlueViolet;
        note.Enabled = true;

        TextBox search = (DetailsView1.FindControl("txtSearch") as TextBox);
        search.BorderStyle = BorderStyle.Solid;
        search.BorderWidth = Unit.Pixel(1);
        search.BorderColor = Color.BlueViolet;
        search.Enabled = true;

        TextBox image = (DetailsView1.FindControl("txtImageFolderName") as TextBox);
        image.BorderStyle = BorderStyle.Solid;
        image.BorderWidth = Unit.Pixel(1);
        image.BorderColor = Color.BlueViolet;
        image.Enabled = true;

        TextBox flash = (DetailsView1.FindControl("txtFlash") as TextBox);
        flash.BorderStyle = BorderStyle.Solid;
        flash.BorderWidth = Unit.Pixel(1);
        flash.BorderColor = Color.BlueViolet;
        flash.Enabled = true;

        TextBox order = (DetailsView1.FindControl("txtOrder") as TextBox);
        order.BorderStyle = BorderStyle.Solid;
        order.BorderWidth = Unit.Pixel(1);
        order.BorderColor = Color.BlueViolet;
        order.Enabled = true;

        TextBox user = (DetailsView1.FindControl("txtUserID") as TextBox);
        user.BorderStyle = BorderStyle.Solid;
        user.BorderWidth = Unit.Pixel(1);
        user.BorderColor = Color.BlueViolet;
        user.Enabled = true;

        DropDownList isActive = DetailsView1.FindControl("ddlIsActivated") as DropDownList;
        isActive.Enabled = true;

        TextBox url = (DetailsView1.FindControl("txtUrl") as TextBox);
        url.BorderStyle = BorderStyle.Solid;
        url.BorderWidth = Unit.Pixel(1);
        url.BorderColor = Color.BlueViolet;
        url.Enabled = true;
    }
}